package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	var randNum int

	CreatNum(&randNum)

	sliceRand := make([]int, 4)

	GetNum(sliceRand, randNum)
	//	fmt.Println("sliceRand=", sliceRand)

	OnGame(sliceRand)

}

func OnGame(sliceRand []int) {
	var num int
	keyNum := make([]int, 4)
	for {
		for {
			fmt.Scan(&num)
			if num > 999 && num < 10000 {
				break
			}
			fmt.Println("请重新输入")
		}

		GetNum(keyNum, num)

		n := 0
		for i := 0; i < len(sliceRand); i++ {
			if keyNum[i] > sliceRand[i] {
				fmt.Printf("第%d位大了一点\n", i+1)
			} else if keyNum[i] < sliceRand[i] {
				fmt.Printf("第%d位小了一点\n", i+1)
			} else {
				n++
				fmt.Printf("第%d位猜对了\n", i+1)
			}
		}
		if n == 4 {
			fmt.Println("全部猜对了")
		}

		//		fmt.Println(keyNum)
		//		fmt.Println(sliceRand)

	}

}

func GetNum(s []int, num int) {
	s[0] = num / 1000
	s[1] = num % 1000 / 100
	s[2] = num % 100 / 10
	s[3] = num % 10
}

func CreatNum(p *int) {
	rand.Seed(time.Now().UnixNano())
	//	randNum := rand.Intn(10000)
	var randNum int

	for {
		randNum = rand.Intn(10000)
		if randNum > 1000 {
			break
		}
	}
	*p = randNum
}
